﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Commercial;

public partial class UserControl_addstore : System.Web.UI.UserControl
{
    Up1VNDataContext db = new Up1VNDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Jobs jobs = new Jobs();
            DropDownList_Job.DataSource = jobs.ListJob();
            DropDownList_Job.AppendDataBoundItems = true;
            DropDownList_Job.DataValueField = "id";
            DropDownList_Job.DataTextField = "job_name";
            DropDownList_Job.DataBind();

            Cities cities = new Cities();
            DropDownList_City.DataSource = cities.ListCity();
            DropDownList_City.DataTextField = "city_name";
            DropDownList_City.DataValueField = "id";
            DropDownList_City.SelectedValue = "2";
            DropDownList_City.DataBind();

            Districts districts = new Districts();
            DropDownList_District.DataSource = districts.ListDistrict(2);
            DropDownList_District.DataTextField = "district_name";
            DropDownList_District.DataValueField = "id";
            DropDownList_District.DataBind();
        }
    }
    protected void Button_AddBrand_Click(object sender, EventArgs e)
    {
        Brand brand = new Brand();
        if (brand.CheckExistBrand(TextBox_BrandName.Text.Trim()) >= 1)
        {
            ClientScriptManager cls = Page.ClientScript;
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('Thương hiệu này đã được đăng ký');");
            cls.RegisterClientScriptBlock(this.GetType(), "alertthuonghieu", sb.ToString(), true);
        }
        else
        {
            int id_district = 0;
            int id_city = Convert.ToInt32(DropDownList_City.SelectedValue);
            if (DropDownList_District.SelectedValue != "")
            {
                id_district = Convert.ToInt32(DropDownList_District.SelectedValue);
            }
            string[] splitFileName = HiddenField_Logo.Value.Split('/');
            string fileName = splitFileName[splitFileName.Length - 1];
            brand.CreateBrand(TextBox_BrandName.Text.Trim(), TextBox_BrandDescription.Text.Trim(), fileName, TextBox_BrandUrl.Text.Trim(), DropDownList_Job.SelectedItem.Value.Trim(), TextBox_BrandCustomer.Text.Trim(), TextBox_BrandCustomerAddress.Text.Trim(), TextBox_Phone.Text.Trim(), TextBox_MobilePhone.Text.Trim(), TextBox_Email.Text.Trim(), TextBox_Fax.Text.Trim(), TextBox_TaxCode.Text.Trim(), 0, 0, id_city, id_district);
            Response.Redirect("~/quanlythuonghieu.html");
        }
    }
    protected void DropDownList_City_SelectedIndexChanged(object sender, EventArgs e)
    {
        int id_city = Convert.ToInt32(DropDownList_City.SelectedValue);
        Districts districts = new Districts();
        DropDownList_District.DataSource = districts.ListDistrict(id_city);
        DropDownList_District.DataTextField = "district_name";
        DropDownList_District.DataValueField = "id";
        DropDownList_District.DataBind();
    }
}